package com.gitee.sop.layui.adminserver.module.background.system.api.isv;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.easyopen.doc.annotation.ApiDoc;
import com.gitee.easyopen.doc.annotation.ApiDocMethod;
import com.gitee.easyopen.util.CopyUtil;
import com.gitee.sop.layui.adminserver.module.background.system.api.isv.param.RoleForm;
import com.gitee.sop.layui.adminserver.module.background.system.api.isv.param.RolePageParam;
import com.gitee.sop.layui.adminserver.module.background.system.api.isv.result.RoleVO;
import com.gitee.sop.layui.adminserver.module.background.system.entity.PermRole;
import com.gitee.sop.layui.adminserver.module.background.system.entity.PermRolePermission;
import com.gitee.sop.layui.adminserver.module.background.system.mapper.PermRoleMapper;
import com.gitee.sop.layui.adminserver.module.background.system.service.PermRolePermissionService;
import com.gitee.sop.layui.adminserver.module.base.constants.Result;
import com.gitee.sop.layui.adminserver.module.base.error.ServiceInterruptionException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author admin
 */
@RestController
@ApiDoc("ISV管理")
@Slf4j
public class RoleApi {

    @Autowired
    PermRoleMapper permRoleMapper;

    @Autowired
    PermRolePermissionService permRolePermissionService;

    @GetMapping("/api/role.listall")
    Result roleListall() {
        List<RoleVO> collect = permRoleMapper.selectList(new QueryWrapper<PermRole>().lambda().orderByAsc(PermRole::getId)).stream()
                .map(permRole -> {
                    RoleVO vo = new RoleVO();
                    CopyUtil.copyProperties(permRole, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        return Result.success(collect);
    }

    @ApiDocMethod(description = "获取角色，分页")
    @GetMapping("/api/role.page")
    Result pageRole(RolePageParam rolePage) {
        Page<PermRole> page = new Page<>(rolePage.getPageIndex(), rolePage.getPageSize());
        LambdaQueryWrapper<PermRole> wrapper = new QueryWrapper<PermRole>().lambda().likeRight(StringUtils.isNotBlank(rolePage.getRoleCode()), PermRole::getRoleCode, rolePage.getRoleCode());
        wrapper.orderByDesc(PermRole::getGmtCreate);
        permRoleMapper.selectPage(page, wrapper);
        List<PermRole> records = page.getRecords();

        List<RoleVO> retList = records.stream()
                .map(role -> {
                    RoleVO vo = new RoleVO();
                    CopyUtil.copyProperties(role, vo);
                    return vo;
                })
                .collect(Collectors.toList());
        return Result.successPage(retList, page.getTotal(), page.getCurrent(), page.getSize());
    }

    @GetMapping("/api/role.get")
    Result getRole(Long id) {
        PermRole rec = permRoleMapper.selectById(id);
        return Result.success(rec);
    }

    @PostMapping("/api/role.add")
    Result addRole(RoleForm roleForm) {
        PermRole rec = permRoleMapper.selectOne(new QueryWrapper<PermRole>().lambda().eq(PermRole::getRoleCode, roleForm.getRoleCode()));
        if (rec != null) {
            throw ServiceInterruptionException.fail(500, "角色码已存在");
        }
        PermRole permRole = new PermRole();
        CopyUtil.copyPropertiesIgnoreNull(roleForm, permRole);
        permRoleMapper.insert(permRole);
        return Result.success();
    }

    @PostMapping("/api/role.update")
    Result updateRole(RoleForm roleForm) {
        PermRole rec = permRoleMapper.selectById(roleForm.getId());
        rec.setDescription(roleForm.getDescription());
        permRoleMapper.updateById(rec);
        return Result.success();
    }

    @PostMapping("/api/role.del")
    Result delRole(long id) {
        PermRole rec = permRoleMapper.selectById(id);
        PermRolePermission rolePermission = permRolePermissionService.getOne(new QueryWrapper<PermRolePermission>().lambda().eq(PermRolePermission::getRoleCode, rec.getRoleCode()));
        if (rolePermission != null) {
            throw ServiceInterruptionException.fail(500, "该角色已使用，无法删除");
        }
        permRoleMapper.deleteById(id);
        return Result.success();
    }
}
